package com.app.juyanresturantapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.app.juyanresturantapp.bean.UsersBean;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegistActivity extends AppCompatActivity {
    Button submit;
    EditText registerTelephone,registerPassword,registerRepeatPassword;
    String telephone,password,repeatPassword;
    String TAG = "RegistActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
        initData();

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                if(!(telephone.equals("")||password.equals("")||repeatPassword.equals(""))){
                    if(password.equals(repeatPassword)){
                        regiter();
                    }else {
                        Toast.makeText(RegistActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                    }

                }else {
                    Toast.makeText(RegistActivity.this, "各项值不能为空", Toast.LENGTH_SHORT).show();
                }



            }
        });
    }

    private void regiter() {
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("telephone",telephone)
                .add("password",password)
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url(getResources().getString(R.string.url)+"/app12/user/register")
                .post(body)
                .build();

        final Call call = client.newCall(request);


        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String data = response.body().string();

                Gson gson = new Gson();
                UsersBean registBean = gson.fromJson(data, UsersBean.class);
                Looper.prepare();
                Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
                Looper.loop();




            }
        });

    }

    private void initData() {
        telephone = registerTelephone.getText().toString();
        password = registerPassword.getText().toString();
        repeatPassword = registerRepeatPassword.getText().toString();
    }

    private void initView() {
        registerTelephone = findViewById(R.id.registerTelephone);
        registerPassword = findViewById(R.id.registerPassword);
        registerRepeatPassword = findViewById(R.id.registerRepeatPassword);
        submit = findViewById(R.id.submit);
    }
}